import publicForm from "@/components/public/el-form.vue"; //弹框form
import formJs from "./regexDetail.js"; //新增弹框配置项
import FormLyout from '@/components/commonPublic/fromLayout.vue'
import actions from "@/actions";
import {
    saveSpecificationData, //保存
    getSpecificationDataById, //详情
    getSpeciesDataList //种类

} from "@/api/request.js";
export default {
    props: [
        // "dialogVisible",
        // "dialogTitle",
        // "statusNum",
        // "currentRow",
    ],
    data() {
        return {
            visible: "",
            loading: false,
            title: null,
            isLock: false,
            dialogHeight: null,
            formObj: formJs.formObj,
            treeData: [], //材料种类
            formData: {
                fId: "", //选中的数据ID(新增不传)
                fVarietyid: "", //种类ID
                fVarietyname: "", //种类名
                fMaterialid: "", //材料ID
                fMaterialname: "", //材料名
                fVersionid: "", //规格型号ID(新建/复制不传)
                fVersion: "", //规格型号名
                fUnit: "", //材料单位
                fFrequency: "", //频率
                fStandard: "", //依据标准
                fNote: "", //检测频率说明
                typ: "", //"1"代表复制
                userId: "", //当前登录账户ID
                userName: "" //当前登录账户名
            },
            btnShow: true,
            currentDetail: null,
            fStatus: 1,
            primaryInfo: null,
            currentDetail: null,
            statusNum: null,
            currentRow: {}
        };
    },
    components: {
        publicForm,
        FormLyout
    },
    // watch: {
    //     dialogVisible: {
    //         handler(newVal) {
    //             if (newVal) {
    //                 this.visible = newVal;
    //             }
    //         },
    //         immediate: true,
    //     },
    //     dialogTitle: {
    //         handler(newVal) {
    //             if (newVal) {
    //                 this.title = newVal;
    //             }
    //         },
    //         immediate: true,
    //     },
    //     statusNum: {
    //         handler(newVal) {
    //             if (newVal) {
    //                 this.edit(newVal);
    //             }
    //         },
    //         immediate: true,
    //     },
    //     currentRow: {
    //         handler(newVal) {
    //             if (newVal) {
    //                 this.currentDetail = newVal;
    //                 if (this.statusNum != 1) {
    //                     this.getDetail({
    //                         fId: this.currentDetail.fId
    //                     })
    //                 }
    //             }
    //         },
    //         immediate: true,
    //     },
    // },
    created() {
        window.addEventListener("resize", this.getHeight);
        this.getHeight();
    }, //获取页面高度自适应：

    destroyed() {
        window.removeEventListener("resize", this.getHeight);
        this.getHeight();
    },
    mounted() {
        this.primaryInfo = JSON.parse(sessionStorage.getItem("GxEmbedded_primaryInfo"))
        this.statusNum = this.primaryInfo.status;

        this.currentDetail = this.primaryInfo.data;

        actions.onGlobalStateChange((state, prev) => {
            var data = prev.currentUser.projects;
            this.userInfo = prev.currentUser
        }, true);
        this.btnShow = true
        this.getSpeciesList() //获取种类材料
        if (this.statusNum == 1) { //新增
            this.formData = this.$options.data.call(this).formData;
            this.btnShow = true
            this.formObj.map((item) => {
                item.disabled = true;
            });
            this.formObj[0].disabled = false;
            this.formObj[1].disabled = false;
            this.formObj[2].disabled = false;
            this.formObj[1].options = [];

        } else if (this.statusNum == 2) { //复制
            this.formData.fId = this.currentDetail.fId //按钮 
            this.formData.typ = "1" //"1"代表复制
            this.formObj.map((item) => {
                item.disabled = true;
            });
            this.formObj[0].disabled = true;
            this.formObj[1].disabled = false;
            this.formObj[2].disabled = false;
        } else if (this.statusNum == 3) { //编辑
            this.formData.fId = this.currentDetail.fId //按钮 
            this.getDetail({
                fId: this.currentDetail.fId
            }) 
            this.formObj.map((item) => {
                item.disabled = true;
            });
            this.formObj[0].disabled = true;
            this.formObj[1].disabled = false;
            this.formObj[2].disabled = false;
        } else if (this.statusNum == 7) { //查看
            this.btnShow = false
            this.formObj.map((item) => {
                item.disabled = true;
            });
            this.getDetail({
                fId: this.currentDetail.fId
            }) 
        }
        this.getHeight();
    },
    methods: {
        getHeight() {
            var h = document.documentElement.clientHeight; //获取页面可见高度
            this.dialogHeight = h * 0.5 + "px";
        },

        closeDialog() {
            this.$emit("close");
        },
        selectChange(data, a, b) {
            if (b == "fVarietyname") {
                this.formData.fVarietyid = data.value;
                this.formData.fVarietyname = data.label;
                this.formData.fMaterialname = ""
                this.formObj[1].options = [];
                data.children.map(item => {
                    this.formObj[1].options.push({
                        label: item.fMaterialname, //种类名
                        value: item.fMaterialid, //种类id
                        fFrequency: item.fFrequency, //检测频率
                        fStandard: item.fStandard, //依据标准
                        fUnit: item.fUnit, //单位
                        fNote: item.fNote,
                    })
                })
            } else if (b == "fMaterialname") {
                this.formData.fMaterialid = data.value;
                this.formData.fMaterialname = data.label;
                this.formData.fFrequency = data.fFrequency;
                this.formData.fStandard = data.fStandard;
                this.formData.fUnit = data.fUnit;
                this.formData.fNote = data.fNote;
            }
        },
        edit() {
            this.formData = this.$options.data.call(this).formData;
        },
        getSpeciesList() {
            var obj = {
                page: 1,
                pageSize: 500
            }
            getSpeciesDataList(obj)
                .then((res) => {
                    if (res.errcode == 0) {
                        this.treeData = []
                        this.formObj[0].options = []
                        res.data.pageInfo.list.map(item => {
                            if (item.fStatus == 1) {
                                this.formObj[0].options.push({
                                    label: item.fVarietyname,
                                    value: item.fVarietyid,
                                    children: item.materialList
                                })
                            }
                        })
                    } else {
                        this.$message.error({
                            content: res.msg,
                            duration: 1000
                        })
                        return false;
                    }
                })
                .catch(() => {
                    return false;
                });
        },
        // 详情
        getDetail(obj) {
            this.loading = true;
            getSpecificationDataById(obj).then((res) => {
                if (res.errcode == 0) {
                    this.loading = false;
                    var data = res.data
                    this.formData = data
                    if (this.statusNum == 2) { //复制
                        this.formData.typ = "1" //"1"代表复制
                    }
                    if (this.statusNum == 7) {
                        this.btnShow = false;
                    } else {
                        this.btnShow = true;
                    }
                } else {
                    this.loading = false;
                    this.$message.error({
                        content: res.msg,
                        duration: 1000
                    })
                }
            });
        },

        yj_saveForm() {
            this.formData.userId = this.userInfo.userId; //当前登录人id
            this.formData.userName = this.userInfo.realName; //当前登录人名称
            // 0连续新增1新增
            if (this.$refs.publicForm.submitForm()) {
                this.saveForm(this.formData);
            } else {
                console.log("error");
            }

        },
        // 保存
        saveForm(data) {
            this.loading = true;
            this.isLock = true
            saveSpecificationData(data)
                .then((res) => {
                    if (res.errcode == 0) {
                        this.loading = false;
                        this.visible = false;
                        // this.$emit("refreshList");
                        this.$router.push({
                            path: "/materialDataManage/specificationManagement",
                        });
                        this.$message.success({
                            content: '新增成功',
                            duration: 1000
                        })
                        this.isLock = false
                    } else if (res.errcode == -1) {
                        this.$message.warning({
                            content: res.data,
                            duration: 1000
                        })
                        this.loading = false;
                        this.isLock = false;
                    } else {
                        this.loading = false;

                        this.isLock = false
                        this.$message.error({
                            content: res.data,
                            duration: 1000
                        })
                    }
                })
        },
    },
};